package weaver.jobs.geely.duss.cron;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.weaver.integration.datesource.SAPInterationOutUtil;
import com.weaver.integration.log.LogInfo;
import weaver.common.DateUtil;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.geely.duss.utils.GeelyUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年11月20日 09:30
 * description:调用金蝶接口，同步基础信息到建模表单
 */
public class WeaSyncKmzsjZ117CronJob extends BaseCronJob {
    @Override
    public void execute() {
        GeelyUtil geelyUtil = new GeelyUtil();
        BaseBean baseBean = new BaseBean();
        try {
            String curdate = DateUtil.getFullDate();
            String tablename = "uf_kmzsj";
            baseBean.writeLog(">>>>>WeaSyncKmzsjZ117CronJob>>>>>curdate=" + curdate);
            SAPInterationOutUtil outUtil = new SAPInterationOutUtil();
            JCO.Client myConnection = (JCO.Client) outUtil.getConnection("1", new LogInfo());
            myConnection.connect();
            // 实例名
            JCO.Repository myRepository = new JCO.Repository("Repository", myConnection);
            //设置BAPI函数
            IFunctionTemplate ft = myRepository.getFunctionTemplate("ZFI_GET_ACCOUNT");
            JCO.Function bapi = ft.getFunction();
            //输入参数和结构处理
            JCO.ParameterList input = bapi.getImportParameterList();
            input.setValue("Z117", "BUKRS");
            //执行函数
            myConnection.execute(bapi);
            //输出参数和结构处理
            //JCO.ParameterList outPrint = bapi.getExportParameterList();
            //输出表的处理
            JCO.ParameterList Table00 = bapi.getTableParameterList();
            int insertCount = 0;
            int updateCount = 0;
            String dataResult = geelyUtil.isExistData("kmdm", tablename);
            List<String> dataList = Arrays.asList(dataResult.split(","));
            //返回处理结果以及日志
            JCO.Table t = Table00.getTable("OUTPUT");
            int totalCount = t.getNumRows();
            for (int i = 0; i < totalCount; i++) {
                Map<String, Object> map = new HashMap<>();
                t.setRow(i);
                String saknr = (String) t.getValue("SAKNR");
                String txt20 = (String) t.getValue("TXT20");
                String ktopl = (String) t.getValue("KTOPL");
                String bukrs = (String) t.getValue("BUKRS");
                String mcod1 = (String) t.getValue("MCOD1");
                String txt50 = (String) t.getValue("TXT50");
                String ktoks = (String) t.getValue("KTOKS");

                if (!dataList.contains(saknr)) {
                    map.put("kmdm", saknr);
                    map.put("kmmc", txt20);
                    map.put("ktopl", ktopl);
                    map.put("bukrs", bukrs);
                    map.put("mcod1", mcod1);
                    map.put("txt50", txt50);
                    map.put("ktoks", ktoks);
                    if (geelyUtil.insertModelData(tablename, map)) {
                        insertCount++;
                    }
                } else {
                    map.put("kmmc", txt20);
                    map.put("ktopl", ktopl);
                    map.put("bukrs", bukrs);
                    map.put("mcod1", mcod1);
                    map.put("txt50", txt50);
                    map.put("ktoks", ktoks);
                    if (geelyUtil.updateModelData(tablename, saknr, map)) {
                        updateCount++;
                    }
                }
            }
            baseBean.writeLog(">>>>>WeaSyncKmzsjZ117CronJob>>>>>总数量：[" + totalCount + "]<br/>");
            baseBean.writeLog(">>>>>WeaSyncKmzsjZ117CronJob>>>>>插入数量：[" + insertCount + "]<br/>");
            baseBean.writeLog(">>>>>WeaSyncKmzsjZ117CronJob>>>>>更新数量：[" + updateCount + "]<br/>");
        } catch (Exception e) {
            baseBean.writeLog("WeaSyncKmzsjZ117CronJob" + e);
            e.printStackTrace();
        }
    }
}
